﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers;

namespace eTMS.Forms.Students
{
    public partial class FrmCanBoLop : eTMS.Controls.BaseForm
    {
        public FrmCanBoLop()
        {
            InitializeComponent();
        }
        CanBoLopController canBoLopController = new CanBoLopController();
        SinhVienController sinhVienController = new SinhVienController();
        private void FrmCanBoLop_Load(object sender, EventArgs e)
        {
            sinhVienList1.AddControlToBottomPanel(new Control[] { btnChon,btnRemove,btnSave});
            treeKhoaNganhKhoaHoc1.InitData();
            ChucVuCanBoLopController.LoadAllChucVuCanBoLop(false);
            ColChucVu.DisplayMember = "TenChucVu";
            ColChucVu.ValueMember = "ChucVuCanBoLop_ID";
            ColChucVu.DataPropertyName = "ChucVuCanBoLop_ID";
            ColChucVu.DataSource = ChucVuCanBoLopController.ListChucVuCanBoLopStatic;
            sinhVienList1.SetController(sinhVienController);
        }


        private void BindSinhVien()
        {
            if (treeKhoaNganhKhoaHoc1.SelectedLop_ID <= 0)
            {
                sinhVienController.Table = new DataTable();
                sinhVienList1.UpdateToScreen();
                gridCanBoLop.AutoGenerateColumns = false;
                gridCanBoLop.DataSource = null;
                return;
            }
            sinhVienController.ResetObject();
            sinhVienController.SinhVienObject.GioiTinh = 2;
            sinhVienController.SearchSinhVien(treeKhoaNganhKhoaHoc1.SelectedKhoa_ID, treeKhoaNganhKhoaHoc1.SelectedNganhHoc_ID, treeKhoaNganhKhoaHoc1.SelectedLop_ID, treeKhoaNganhKhoaHoc1.KhoaHoc_ID);
            sinhVienList1.UpdateToScreen();
            canBoLopController.LoadAllDSCanBoLop(treeKhoaNganhKhoaHoc1.SelectedLop_ID);
            gridCanBoLop.AutoGenerateColumns = false;
            gridCanBoLop.DataSource = canBoLopController.Table;
        }
        private void treeKhoaNganhKhoaHoc1_OnChangeTreeSelected(object sender, EventArgs e)
        {
            BindSinhVien();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            canBoLopController.SaveCanBoLop();
            ShowInfo(eTMS.Commons.Constants.MSS_SAVE_SUCCESS);
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            canBoLopController.AddCanBoLop(sinhVienList1.GetSelectedSinhVienRows());
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            List<int> ls = new List<int>();
            foreach (DataGridViewRow r in gridCanBoLop.SelectedRows)
            {
                ls.Add(r.Index);
            }
            foreach (DataGridViewCell r in gridCanBoLop.SelectedCells)
            {
                if (!ls.Contains(r.RowIndex))
                    ls.Add(r.RowIndex);
            }
            if (ls.Count > 0)
            {
                List<DataRow> lsRow = new List<DataRow>();
                foreach (int index in ls)
                {
                    lsRow.Add(((DataRowView)gridCanBoLop.Rows[index].DataBoundItem).Row);
                }
                canBoLopController.RemoveCanBoLop(lsRow);
            }
        }
    }
}
